home *** CD-ROM | disk | FTP | other *** search
/ Aminet 30 / Aminet 30 (1999)(Schatztruhe)[!][Apr 1999].iso / Aminet / util / arc / LZHUtils_src.lha / ScanLZH.c < prev    next >
C/C++ Source or Header  |  1997-06-05  |  3KB  |  133 lines

  1. /*************************************************
  2.     ScanLZH : Scan files in LHA's archive
  3.     
  4.     Copyright © 1994 Manolis S Pappas.
  5.     All rights reserved.
  6.     
  7. *************************************************/
  8.  
  9. #include <stdio.h>
  10. #include <string.h>
  11.  
  12. #define BUFSZ 4096
  13. #define OVER  4096
  14. #define VERSION "1.1"
  15. #define CLI_VERSION "$VER: ScanLZH v1.1"
  16.  
  17. char name[256];
  18. unsigned char buf[BUFSZ + OVER];
  19. unsigned char id[5] = {'-', 'l', 'h', '?', '-'};
  20.  
  21. void exthdr(unsigned char *header)
  22. {
  23.     int i;
  24.  
  25. #if 0
  26.     while ((i = header[-2] + header[-1] * 256) != 0) {
  27. #else
  28.     /* limits extended header upto 255 bytes */
  29.     while (header[-1] == 0 && (i = header[-2]) != 0) {
  30. #endif
  31.         if (header[0] == 1) {
  32.             strncpy(name, &header[1], i - 3);
  33.         }
  34.         header += i;
  35.     }
  36. }
  37.  
  38. void main(int argc, char *argv[])
  39. {
  40.     FILE *f;
  41.     unsigned long a, b, old_b, pos;
  42.     int  c, s, i, left, comp_max;
  43.     unsigned char *p, *comp_max_pos, *header;
  44.  
  45.     strcpy(name, argv[1]);
  46.     f = fopen(name, "rb");
  47.     if (f == NULL) {
  48.         strcat(name, ".lzh");
  49.         f = fopen(name, "rb");
  50.         if (f == NULL) {
  51.             printf("ScanLZH v%s\n",VERSION);
  52.             printf("Copyright (©) 1994-95, The Xperts Group Inc.\n");
  53.                         printf("All Rights Reserved Worldwide.\n");
  54.                         printf("Author: Manolis S Pappas.\n\n");
  55.             fprintf(stderr, "Usage : %s bad-lzh [ >info-file ]\n",argv[0]);
  56.             return;
  57.         }
  58.     }
  59.     left = fread(buf, 1, BUFSZ + OVER, f);
  60.     pos = 0;
  61.     s = old_b = 0;
  62.     do {
  63.         comp_max = (left < BUFSZ) ? left : BUFSZ;
  64.         comp_max_pos = buf + comp_max + 2;
  65.         for (p = buf + 2; p < comp_max_pos; p++) {
  66.             s = 0;
  67.             do {
  68.                 c = p[s];
  69.                 switch (s) {
  70.                 case 0:
  71.                 case 1:
  72.                 case 2:
  73.                 case 4:
  74.                     if (c == id[s]) {
  75.                         s++;
  76.                     } else {
  77.                         s = 0;
  78.                     }
  79.                     break;
  80.                 case 3:
  81.                     if (c >= '0' && c <= '9' || c == 'd') {
  82.                         s++;
  83.                     } else {
  84.                         s = 0;
  85.                     }
  86.                     break;
  87.                 case 5:
  88.                     memset(name, 0, sizeof(name));
  89.                     header = p - 2;
  90.                     a = pos + (header - buf);
  91.                     b = header[10];
  92.                     b = b * 256 + header[9];
  93.                     b = b * 256 + header[8];
  94.                     b = b * 256 + header[7] + a;
  95.                     switch (header[20]) {
  96.                     case 0:
  97.                     case 1:
  98.                         i = header[0] + 2;
  99.                         b += i;
  100.                         strncpy(name, &header[22], header[21]);
  101.                         if (header[20] == 1) {
  102.                             exthdr(header + i);
  103.                         }
  104.                         break;
  105.                     case 2:
  106.                         b += header[1] * 256 + header[0];
  107.                         exthdr(header + 26);
  108.                         break;
  109.                     default:
  110.                         s = 0;
  111.                     }
  112.                     if (s) {
  113.                         if (old_b != a) printf("=================\n");
  114.                         printf("%08lx %08lx %s\n", a, b, name);
  115.                         old_b = b;
  116.                         s = 0;
  117.                     }
  118.                     break;
  119.                 }
  120.             } while (s);
  121.         }
  122.         memcpy(buf, buf + comp_max, OVER);
  123.         pos += comp_max;
  124.         left -= comp_max;
  125.     } while (left += fread(buf + OVER, 1, BUFSZ, f));
  126.     fclose(f);
  127. }
  128.  
  129. void do_nothing()
  130. {
  131.         printf("%s",CLI_VERSION);
  132. }
  133.